package com.example.demo.Controller.User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.Bean.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Entity.Login.LogInBackUser;
import com.example.demo.ServiceImpl.User.UserServiceImpl;
import com.example.demo.Entity.User.User;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserServiceImpl userService;

    @PostMapping
    public Boolean save(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable(value = "id") Integer id) {
        return userService.removeById(id);
    }

    @GetMapping
    public R findAll() {
        return R.ok().put("userList", userService.list());
    }

    @GetMapping("/{id}")
    public R findOne(@PathVariable(value = "id") Integer id) {
        return R.ok().put("user", userService.getById(id));
    }

    @GetMapping("/page")
    public R findPage(@RequestParam(value = "pageNum") Integer pageNum,
                      @RequestParam(value = "pageSize") Integer pageSize) {
        return R.ok().put("userPage", userService.page(new Page<>(pageNum, pageSize)));
    }
    @Operation(summary = "用户登录后台")
    @PostMapping("/login")
    public R login(@RequestBody LogInBackUser user) {
        return userService.LogInBack(user);
    }

    @PostMapping("/login/wx")
    @Operation(summary = "用户登录")
    public R login(@RequestBody User user, HttpServletRequest httpServletRequest) {
        HttpSession session = httpServletRequest.getSession();
        LambdaQueryWrapper<User>userLambdaQueryWrapper= Wrappers.lambdaQuery();
        userLambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        if(userService.getOne(userLambdaQueryWrapper)!=null||userService.getOne(userLambdaQueryWrapper).getPassword().equals(user.getPassword())) {
            user=userService.getOne(userLambdaQueryWrapper);
            session.setAttribute("uid", user.getId());
            return R.ok().put("user",user);
        }else{
            return R.error();
        }
    }
    @PostMapping("/SigIn")
    @Operation(summary = "用户注册")
    public R sigin(@RequestBody LogInBackUser user) {
        LambdaQueryWrapper<User>userLambdaQueryWrapper= Wrappers.lambdaQuery();
        userLambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        if(userService.count(userLambdaQueryWrapper)==0) {
            userService.saveOrUpdate(new User(null,user.getUsername(),user.getPassword(),"普通用户","女","","","https://img.zcool.cn/community/014f685f110b9fa801215aa096689e.png@1280w_1l_2o_100sh.png","普通用户"));
            return R.ok();
        }else{
            System.out.println(userService.count(userLambdaQueryWrapper));
            return R.error();
        }
    }

    @Operation(summary = "byd管理系统授权")
    @GetMapping("/info")
    public R info(@RequestParam Integer token) {
        User user=userService.getById(token);
        R r = R.ok();
        List<String> roles = new ArrayList<>();
        roles.add(user.getType());
        r.put("roles", roles);
        r.put("name", user.getName());
        r.put("avatar", user.getUrl());
        r.put("introduction", "海内存知己，天涯若比邻");
        return r;
    }
    @Operation(summary = "模糊查询用户")
    @GetMapping("/like/{text}")
    public R like(@PathVariable(value = "text") String text) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper= Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(User::getName,text);
        userLambdaQueryWrapper.eq(User::getType,"老师");
        return R.ok().put("userList", userService.list(userLambdaQueryWrapper));
    }
}